package com.itheima.reggie.common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.itheima.reggie.entity.OrderDetail;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.entity.ShoppingCart;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * <b><code>MyMetaObjectHandler</code></b>
 * <p/>
 * 自定义元数据对象处理器
 * <p/>
 * <b>Creation Time:</b> 24/06/2024 11:04.
 *
 * @author yanjr
 * @since reggie_take_out
 */
@Component
@Slf4j
public class MyMetaObjectHandler implements MetaObjectHandler {

    /**
     * 插入操作自动填充
     * @param metaObject 元数据
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        metaObject.setValue("createTime", LocalDateTime.now());
        if (!(metaObject.getOriginalObject() instanceof ShoppingCart || metaObject.getOriginalObject() instanceof Orders
                || metaObject.getOriginalObject() instanceof OrderDetail)) {
            metaObject.setValue("updateTime", LocalDateTime.now());
            metaObject.setValue("createUser", BaseContext.getCurrentUser());
            metaObject.setValue("updateUser", BaseContext.getCurrentUser());
        }
    }

    /**
     * 更新操作自动填充
     * @param metaObject 元数据
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        metaObject.setValue("updateTime", LocalDateTime.now());
        metaObject.setValue("updateUser", BaseContext.getCurrentUser());
    }
}
